Public Class Correo
    Private m_ServidorCorreo As String
    Private m_De As String
    Private m_Para() As String
    Private m_ConCopia() As String
    Private m_ConCopiaOculta() As String
    Private m_Asunto As String
    Private m_Mensaje As String
    Private m_Adjuntos() As String
    Private m_Prioridad As System.Net.Mail.MailPriority
    Private m_MensajeEsHTML As Boolean
    Private m_MensajeError As String
    Private m_Credencial As System.Net.NetworkCredential

    Public Sub New(ByVal Servidor As String)
        m_ServidorCorreo = Servidor
        m_Prioridad = Net.Mail.MailPriority.Normal
        m_MensajeEsHTML = False
        m_MensajeError = String.Empty
    End Sub

    Public Sub New(ByVal Servidor As String, ByVal CredencialEnvio As System.Net.NetworkCredential)
        m_ServidorCorreo = Servidor
        m_Prioridad = Net.Mail.MailPriority.Normal
        m_MensajeEsHTML = False
        m_MensajeError = String.Empty

        m_Credencial = CredencialEnvio
    End Sub

    Public Property MensajeEsHTML() As Boolean
        Get
            Return m_MensajeEsHTML
        End Get
        Set(ByVal value As Boolean)
            m_MensajeEsHTML = value
        End Set
    End Property

    Public Property Credencial() As System.Net.NetworkCredential
        Get
            Return m_Credencial
        End Get
        Set(ByVal value As System.Net.NetworkCredential)
            m_Credencial = value
        End Set
    End Property

    Public Property De() As String
        Get
            Return m_De
        End Get
        Set(ByVal value As String)
            m_De = value
        End Set
    End Property

    Public Property CC() As String()
        Get
            Return m_ConCopia
        End Get
        Set(ByVal value() As String)
            m_ConCopia = value
        End Set
    End Property

    Public Property CCO() As String()
        Get
            Return m_ConCopiaOculta
        End Get
        Set(ByVal value() As String)
            m_ConCopiaOculta = value
        End Set
    End Property

    Public Property Para() As String()
        Get
            Return m_Para
        End Get
        Set(ByVal value() As String)
            m_Para = value
        End Set
    End Property

    Public Property Asunto() As String
        Get
            Return m_Asunto
        End Get
        Set(ByVal value As String)
            m_Asunto = value
        End Set
    End Property

    Public Property Mensaje() As String
        Get
            Return m_Mensaje
        End Get
        Set(ByVal value As String)
            m_Mensaje = value
        End Set
    End Property

    Public ReadOnly Property MensajeError() As String
        Get
            Return m_MensajeError
        End Get
    End Property

    Public Function Enviar() As Boolean
        Dim Retorno As Boolean = False
        Try
            Retorno = Enviar(m_ServidorCorreo, m_Credencial, m_De, m_Para, m_ConCopia, m_ConCopiaOculta, m_Asunto, m_Mensaje, m_Adjuntos, m_Prioridad, m_MensajeEsHTML)
        Catch ex As Exception
            m_MensajeError = ex.Message
        End Try

        Return Retorno
    End Function

    Public Function Enviar(ByVal De As String, ByVal Para() As String, ByVal Asunto As String, ByVal Mensaje As String) As Boolean
        Return Enviar(De, Para, Asunto, Mensaje, Net.Mail.MailPriority.Normal, False)
    End Function

    Public Function Enviar(ByVal De As String, ByVal Para() As String, ByVal Asunto As String, ByVal Mensaje As String, ByVal Prioridad As System.Net.Mail.MailPriority, ByVal MensajeEsHTML As Boolean) As Boolean
        Dim Retorno As Boolean = False


        m_De = De
        m_Para = Para
        m_Asunto = Asunto
        m_Mensaje = Mensaje
        m_Prioridad = Prioridad
        m_ConCopia = Nothing
        m_ConCopiaOculta = Nothing
        m_Adjuntos = Nothing
        m_MensajeEsHTML = MensajeEsHTML

        Try
            Retorno = Enviar(m_ServidorCorreo, m_Credencial, m_De, m_Para, m_ConCopia, m_ConCopiaOculta, m_Asunto, m_Mensaje, m_Adjuntos, m_Prioridad, m_MensajeEsHTML)
        Catch ex As Exception
            m_MensajeError = ex.Message
        End Try

        Return Retorno
    End Function

    Private Shared Function Enviar(ByVal ServidorCorreo As String, ByVal Credencial As System.Net.NetworkCredential, ByVal De As String, ByVal Para() As String, ByVal CC() As String, ByVal CCO() As String, ByVal Asunto As String, ByVal Mensaje As String, ByVal Adjuntos() As String, ByVal Prioridad As System.Net.Mail.MailPriority, ByVal MensajeEsHTML As Boolean) As Boolean
        Dim msg As System.Net.Mail.MailMessage
        Dim Retorno As Boolean = False

        Try
            msg = New System.Net.Mail.MailMessage

            With msg
                .From = New System.Net.Mail.MailAddress(De)

                If Not Para Is Nothing Then
                    For Each strPara As String In Para
                        .To.Add(strPara.trim)
                    Next
                End If

                If Not CC Is Nothing Then
                    For Each strCC As String In CC
                        .CC.Add(strCC)
                    Next
                End If

                If Not CCO Is Nothing Then
                    For Each strCCO As String In CCO
                        .Bcc.Add(strCCO)
                    Next
                End If

                .Subject = Asunto
                .Body = Mensaje
                .IsBodyHtml = MensajeEsHTML
                .Priority = Prioridad

                If Not Adjuntos Is Nothing Then
                    For Each strAdjunto As String In Adjuntos
                        msg.Attachments.Add(New System.Net.Mail.Attachment(strAdjunto))
                    Next
                End If
            End With

            Dim smtp As New System.Net.Mail.SmtpClient(ServidorCorreo)

            smtp.Credentials = Credencial
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network
            smtp.Send(msg)

            msg = Nothing

            Retorno = True

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

        Return Retorno
    End Function
End Class
